package com.test.restful.controller;

import com.test.restful.po.Test;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/test")
public class testController {

    @PostMapping("/add")
    public String add(@RequestBody @Valid Test test, BindingResult bindingResult){
        List<FieldError> list = bindingResult.getFieldErrors();
        List<String> messageList = new ArrayList<String>();
        for(FieldError fieldError:list){
            messageList.add(fieldError.getDefaultMessage());
        }
        return messageList.toString();
    }
}
